package com.tange.module.device.feature;

import android.os.Parcel;
import android.os.Parcelable;
import android.text.TextUtils;

import androidx.annotation.NonNull;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.appbase.custom.base.DeviceFeatureSettings;
import com.tg.app.camera.AVIOCTRLDEFs;
import com.tange.base.toolkit.StringUtils;
import com.tg.appcommon.android.TGLog;

import java.math.BigDecimal;
import java.util.Arrays;
import java.util.List;
import java.util.Map;

import io.objectbox.annotation.Entity;
import io.objectbox.annotation.Id;
import io.objectbox.annotation.Transient;

/**
 * Describe:
 * Created by Rance Rang on 2019-08-15.
 * <p>
 * Copyright (c) Tange.Inc All rights reserved.
 */

@Entity
public class DeviceFeature implements Parcelable {

    public static final String FEATURE_TYPE_AUTOTRACKING = "AutoTracking";
    public static final String FEATURE_TYPE_SUPPORTPTZ = "SupportPTZ";
    public static final String FEATURE_TYPE_DAYNIGHT = "DayNight";
    public static final String FEATURE_TYPE_DOUBLELIGHT = "DoubleLight";
    public static final String FEATURE_TYPE_MICROPHONE = "Microphone";
    public static final String FEATURE_TYPE_ALERTSOUND = "AlertSound";
    public static final String FEATURE_TYPE_MOTION_DETECTION = "MD-Capabilities";
    public static final String FEATURE_TYPE_CAP_DEFENCE = "Cap-Defence";
    public static final String FEATURE_TYPE_CAP_ZOOM = "Cap-Zoom";
    public static final String FEATURE_TYPE_CAP_AI = "Cap-AI";
    public static final String FEATURE_TYPE_CAP_INSTRUCTIONS = "ExtInstructions";
    public static final String FEATURE_TYPE_MULTICHANNELS = "MultiChannels";
    public static final String FEATURE_SUPPORT_YES = "Yes";
    public static final String FEATURE_SUPPORT_NO = "No";
    public static final String FEATURE_SUPPORT_TIMER = "timer";
    public static final String FEATURE_SUPPORT_DIM = "dim";
    public static final String FEATURE_SOLAR = "Solar";
    public static final String FEATURE_WATCH_POS = "WatchPos";
    public static final String FEATURE_TYPE_DEVICE_TYPE = "DeviceType";
    public static final String FEATURE_G_SENSOR = "G-Sensor"; // 是否支持G-Sensor
    public static final String FEATURE_TYPE_SPEAKER = "Speaker";
    public static final String FEATURE_SUPPORT_TUNEVOL = "TuneVol";
    public static final String FEATURE_SUPPORT_MUTEABLE = "Muteable";
    public static final String FEATURE_TYPE_PIR = "PIR";
    public static final String FEATURE_TYPE_ALARM_LIGHT = "AlarmLight";
    public static final String FEATURE_TYPE_RESOLUTION = "Resolutions";
    public static final String FEATURE_TYPE_BATTERYCAM = "BatteryCam";
    public static final String FEATURE_TYPE_RESET = "Reset";
    public static final String FEATURE_TYPE_ROTATEVIDEO = "RotateVideo";
    public static final String FEATURE_TYPE_ROTATEVIDEO_CW90 = "CW90";
    public static final String FEATURE_TYPE_ROTATEVIDEO_CW180 = "CW180";
    public static final String FEATURE_TYPE_ROTATEVIDEO_CW270 = "CW270";
    public static final String SETTINGS_INFO_SDCARD_FREE = "SDFree";
    public static final String SETTINGS_INFO_SDCARD_TOTAL = "SDTotal";
    public static final String SETTINGS_INFO_DEVICE_TYPE = "DeviceType";
    public static final String SETTINGS_INFO_VENDOR = "vendor";
    public static final String SETTINGS_INFO_VERSION = "version";
    public static final String SETTINGS_INFO_STREAM = "streamQuality";
    public static final String SETTINGS_INFO_SDRECORD = "SDRecordMode";//sd录像模式
    public static final String SETTINGS_INFO_MIRROR = "mirrorMode";//画面翻转
    public static final String SETTINGS_INFO_FREQUNENCY = "PowerFrequency"; //防闪烁 、频率
    public static final String SETTINGS_INFO_TRACK = "trackMode";
    public static final String SETTINGS_INFO_NIGHT_VISION = "nightVision";
    public static final String SETTINGS_INFO_DOUBLE_LIGHT_MODE = "doubleLightMode";
    public static final String SETTINGS_INFO_DOUBLE_LIGHT = "doubleLightStatus";
    public static final String SETTINGS_INFO_CLOUD_VIDEO_QUALITY = "CVideoQuality";  //云视频录像分辨率
    public static final String SETTINGS_INFO_MICROPHONE = "Microphone";  //麦克风
    public static final String SETTINGS_INFO_BUZZER = "Buzzer";  //警铃
    public static final String SETTINGS_INFO_ALERTSOUND = "AlertSound";  //警铃
    public static final String SETTINGS_INFO_MOTION_DETECTION = "MD-Capabilities";  //移动侦测
    public static final String SETTINGS_INFO_DEVICE_STATUS = "DeviceStatus";  //开关
    public static final String SETTINGS_INFO_MULTICHANNELS = "MultiChannels";  //开关
    public static final String SETTINGS_INFO_RECORDCONF = "RecordConf";
    public static final String SETTINGS_INFO_RECORDCONF_VALUS = "res";
    public static final String SETTINGS_INFO_TIME_LAPSE_VALUS = "time-lapse";
    public static final String FEATURE_SUPPORT_AUTO = "Auto";
    public static final String FEATURE_SUPPORT_PRESETPOS = "PresetPos";
    public static final String FEATURE_SUPPORT_CRUISE = "Cruise";

    public static final String FEATURE_SUPPORT_SPEAKER = "Speaker";

    public static final String FEATURE_HORZONLY = "HorzOnly";
    public static final String FEATURE_VERTONLY = "VertOnly";
    public static final String FEATURE_LOCINPIC = "LocInPic";
    public static final String QUALITY_SD = "sd";
    public static  final String QUALITY_HD = "hd";
    public static  final String QUALITY_FHD = "fhd";
    public static  final String QUALITY_UHD = "uhd";
    public static  final String QUALITY_FUD = "fud";
    public static  final String QUALITY_WQHD = "wqhd";
    @Id
    public long id;//设备ID
    public String uuid;//设备uuid
    public boolean autoTracking;
    public boolean supportPTZ;
    public boolean watchPos = false;
    public boolean dayNight;
    public boolean doubleLight;
    public boolean doubleLightDim;
    public boolean doubleLightTimer;
    public boolean supportMicrophone = true;
    public boolean supportBuzzer;
    public boolean supportMotionDetect;
    public boolean supportCapDefence;
    public boolean supportZoom;
    public boolean support2Lenses;
    public String zoomMode;
    public boolean supportCloseDevice;
    public boolean supportAlarmBell;
    public boolean supportPbrate;
    public boolean supportAlarmTone;
    public boolean supportDetectZone;
    public boolean supportDetectZoneVisible;
    public boolean supportMultiChannels;
    public boolean isDriveRec;
    public boolean isSupportCarParkingMonitor; // 支持停车监控
    public boolean isSupportSettingCarParkingMonitor; // 支持停车监控
    public boolean recordConf;
    public boolean supportTimeLapse; // 支持延时缩影摄像
    public boolean supportSpeaker = true;
    public boolean supportSpeakerTune;
    public boolean supportMicrophoneTune;
    public boolean supportAlarmLight;
    public boolean supportPIR;
    public boolean supportStatusLed;
    public boolean supportFHD;
    public String supportResolutionsData;
    @Transient
    private List<String> supportResolutions;
    public boolean supportAiSwitch;
    public boolean supportDormant;
    public boolean isBatteryCam; // 是否是电池摄像机
    public boolean supportMicrophoneMuteable;
    public boolean supportIpConfig;
    public boolean supportTemper = false;
    public boolean supportHumidity = false;
    public boolean supportPTZ2 = false; //第二个通道是否支持平台
    public float zoomPos = 0;
    public int currentPlayer = 0;
    public String capDefence;
    @Transient
    private ZoomFeature zoomFeature;
    public boolean dayNightAuto;
    public String capAI;

    public boolean presetPos = false;
    public boolean presetPos2 = false;
    public boolean ptzReset = false;
    public boolean ptzReset2 = false;
    public boolean noShortcut = false;
    public boolean cruise = false;
    public boolean mfocus = false;
    public String zoomMode2;
    @Transient
    private ZoomFeature zoomFeature2;
    public float zoomPos2;

    public String microphoneInfo;

    public int orientation = -1; //-1 没云台 0 水平 1 垂直 2 全方位
    public int orientation2 = -1; //-1 没云台 0 水平 1 垂直 2 全方位


    public int  lightId = 0;        ///< 灯标识: 0
    public int  lightFMask = 0;
    public boolean lightOn;
    public boolean lightMode;
    public boolean lightDelayShutdown;
    public byte lightIntensity = 0;

    public String rotateVideo = "Yes";

    public boolean noMedia;
    public boolean isAec;

    public byte recordFun = 0; //0 没录像功能 非0有功能； 1 没录像 2 录像中

    public boolean supportDeviceBroadcast;
    public boolean supportLocInPic ;
    public boolean supportLocInPic2 ;
    public DeviceFeature() {

    }

    public void setRecordFun(byte record){
        this.recordFun = record;
    }

    public byte getRecordFun(){
        return this.recordFun;
    }

    protected DeviceFeature(Parcel in) {
        id = in.readLong();
        uuid = in.readString();
        autoTracking = in.readByte() != 0;
        supportPTZ = in.readByte() != 0;
        watchPos = in.readByte() != 0;
        dayNight = in.readByte() != 0;
        doubleLight = in.readByte() != 0;
        doubleLightDim = in.readByte() != 0;
        doubleLightTimer = in.readByte() != 0;
        supportMicrophone = in.readByte() != 0;
        supportBuzzer = in.readByte() != 0;
        supportMotionDetect = in.readByte() != 0;
        supportCapDefence = in.readByte() != 0;
        supportZoom = in.readByte() != 0;
        support2Lenses = in.readByte() != 0;
        zoomMode = in.readString();
        supportCloseDevice = in.readByte() != 0;
        supportAlarmBell = in.readByte() != 0;
        supportPbrate = in.readByte() != 0;
        supportAlarmTone = in.readByte() != 0;
        supportDetectZone = in.readByte() != 0;
        supportDetectZoneVisible = in.readByte() != 0;
        supportMultiChannels = in.readByte() != 0;
        isDriveRec = in.readByte() != 0;
        isSupportCarParkingMonitor = in.readByte() != 0;
        isSupportSettingCarParkingMonitor = in.readByte() != 0;
        recordConf = in.readByte() != 0;
        supportTimeLapse = in.readByte() != 0;
        supportSpeaker = in.readByte() != 0;
        supportSpeakerTune = in.readByte() != 0;
        supportMicrophoneTune = in.readByte() != 0;
        supportAlarmLight = in.readByte() != 0;
        supportPIR = in.readByte() != 0;
        supportStatusLed = in.readByte() != 0;
        supportFHD = in.readByte() != 0;
        supportResolutionsData = in.readString();
        supportResolutions = in.createStringArrayList();
        supportAiSwitch = in.readByte() != 0;
        supportDormant = in.readByte() != 0;
        isBatteryCam = in.readByte() != 0;
        supportMicrophoneMuteable = in.readByte() != 0;
        supportIpConfig = in.readByte() != 0;
        supportTemper = in.readByte() != 0;
        supportHumidity = in.readByte() != 0;
        supportPTZ2 = in.readByte() != 0;
        zoomPos = in.readFloat();
        currentPlayer = in.readInt();
        capDefence = in.readString();
        dayNightAuto = in.readByte() != 0;
        capAI = in.readString();

        presetPos = in.readByte() != 0;
        presetPos2 = in.readByte() != 0;
        ptzReset = in.readByte() != 0;
        ptzReset2 = in.readByte() != 0;
        noShortcut = in.readByte() != 0;

        zoomMode2 = in.readString();
        zoomPos2 = in.readFloat();

        microphoneInfo = in.readString();

        orientation = in.readInt();
        orientation2 = in.readInt();
        lightId = in.readInt();
        lightFMask = in.readInt();
        lightOn = in.readByte() != 0;
        lightMode = in.readByte() != 0;
        lightDelayShutdown = in.readByte() != 0;
        lightIntensity = in.readByte();

        rotateVideo = in.readString();
        noMedia = in.readByte() != 0;
        isAec = in.readByte() != 0;

        recordFun = in.readByte();

        supportDeviceBroadcast = in.readByte() != 0;
        supportLocInPic = in.readByte() != 0;
        supportLocInPic2 = in.readByte() != 0;
    }

    @Override
    public void writeToParcel(Parcel dest, int flags) {
        dest.writeLong(id);
        dest.writeString(uuid);
        dest.writeByte((byte) (autoTracking ? 1 : 0));
        dest.writeByte((byte) (supportPTZ ? 1 : 0));
        dest.writeByte((byte) (watchPos ? 1 : 0));
        dest.writeByte((byte) (dayNight ? 1 : 0));
        dest.writeByte((byte) (doubleLight ? 1 : 0));
        dest.writeByte((byte) (doubleLightDim ? 1 : 0));
        dest.writeByte((byte) (doubleLightTimer ? 1 : 0));
        dest.writeByte((byte) (supportMicrophone ? 1 : 0));
        dest.writeByte((byte) (supportBuzzer ? 1 : 0));
        dest.writeByte((byte) (supportMotionDetect ? 1 : 0));
        dest.writeByte((byte) (supportCapDefence ? 1 : 0));
        dest.writeByte((byte) (supportZoom ? 1 : 0));
        dest.writeByte((byte) (support2Lenses ? 1 : 0));
        dest.writeString(zoomMode);
        dest.writeByte((byte) (supportCloseDevice ? 1 : 0));
        dest.writeByte((byte) (supportAlarmBell ? 1 : 0));
        dest.writeByte((byte) (supportPbrate ? 1 : 0));
        dest.writeByte((byte) (supportAlarmTone ? 1 : 0));
        dest.writeByte((byte) (supportDetectZone ? 1 : 0));
        dest.writeByte((byte) (supportDetectZoneVisible ? 1 : 0));
        dest.writeByte((byte) (supportMultiChannels ? 1 : 0));
        dest.writeByte((byte) (isDriveRec ? 1 : 0));
        dest.writeByte((byte) (isSupportCarParkingMonitor ? 1 : 0));
        dest.writeByte((byte) (isSupportSettingCarParkingMonitor ? 1 : 0));
        dest.writeByte((byte) (recordConf ? 1 : 0));
        dest.writeByte((byte) (supportTimeLapse ? 1 : 0));
        dest.writeByte((byte) (supportSpeaker ? 1 : 0));
        dest.writeByte((byte) (supportSpeakerTune ? 1 : 0));
        dest.writeByte((byte) (supportMicrophoneTune ? 1 : 0));
        dest.writeByte((byte) (supportAlarmLight ? 1 : 0));
        dest.writeByte((byte) (supportPIR ? 1 : 0));
        dest.writeByte((byte) (supportStatusLed ? 1 : 0));
        dest.writeByte((byte) (supportFHD ? 1 : 0));
        dest.writeString(supportResolutionsData);
        dest.writeStringList(supportResolutions);
        dest.writeByte((byte) (supportAiSwitch ? 1 : 0));
        dest.writeByte((byte) (supportDormant ? 1 : 0));
        dest.writeByte((byte) (isBatteryCam ? 1 : 0));
        dest.writeByte((byte) (supportMicrophoneMuteable ? 1 : 0));
        dest.writeByte((byte) (supportIpConfig ? 1 : 0));
        dest.writeByte((byte) (supportTemper ? 1 : 0));
        dest.writeByte((byte) (supportHumidity ? 1 : 0));
        dest.writeByte((byte) (supportPTZ2 ? 1 : 0));
        dest.writeFloat(zoomPos);
        dest.writeInt(currentPlayer);
        dest.writeString(capDefence);
        dest.writeByte((byte) (dayNightAuto ? 1 : 0));
        dest.writeString(capAI);

        dest.writeByte((byte) (presetPos ? 1 : 0));
        dest.writeByte((byte) (presetPos2 ? 1 : 0));
        dest.writeByte((byte) (ptzReset ? 1 : 0));
        dest.writeByte((byte) (ptzReset2 ? 1 : 0));
        dest.writeByte((byte) (noShortcut ? 1 : 0));
        dest.writeString(zoomMode2);
        dest.writeFloat(zoomPos2);
        dest.writeString(microphoneInfo);

        dest.writeInt(orientation);
        dest.writeInt(orientation2);
        dest.writeInt(lightId);
        dest.writeInt(lightFMask);
        dest.writeByte((byte) (lightOn ? 1 : 0));
        dest.writeByte((byte) (lightMode ? 1 : 0));
        dest.writeByte((byte) (lightDelayShutdown ? 1 : 0));
        dest.writeByte(lightIntensity);

        dest.writeString(rotateVideo);
        dest.writeByte((byte) (noMedia ? 1 : 0));
        dest.writeByte((byte) (isAec ? 1 : 0));
        dest.writeByte(recordFun);

        dest.writeByte((byte) (supportLocInPic ? 1 : 0));
        dest.writeByte((byte) (supportLocInPic2 ? 1 : 0));
    }

    @Override
    public int describeContents() {
        return 0;
    }

    public static final Creator<DeviceFeature> CREATOR = new Creator<DeviceFeature>() {
        @Override
        public DeviceFeature createFromParcel(Parcel in) {
            return new DeviceFeature(in);
        }

        @Override
        public DeviceFeature[] newArray(int size) {
            return new DeviceFeature[size];
        }
    };

    public static DeviceFeature newDeviceFeature(DeviceFeatureSettings.DeviceFeatureSettingsResp resp) {
        if (resp == null || resp.feature == null) return null;
        DeviceFeature feature = new DeviceFeature();
        for (Map.Entry<String, String> entry : resp.feature.entrySet()) {
            String keys = entry.getKey();
            String values = entry.getValue();
            if (TextUtils.isEmpty(values)) continue;
            values = values.toLowerCase();
            boolean isYes = FEATURE_SUPPORT_YES.toLowerCase().equals(values);
            TGLog.d("=== values = " + values + ", keys = " + keys);
            switch (keys) {
                case SETTINGS_INFO_RECORDCONF:
                    TGLog.d("[DeviceFeature][Parse-RecordConf] values = " + values);
                    feature.recordConf = false;
                    feature.supportTimeLapse = false;

                    if (!TextUtils.isEmpty(values)){
                        if (values.contains(SETTINGS_INFO_TIME_LAPSE_VALUS)){
                            feature.supportTimeLapse = true;
                        }
                        if (values.contains(SETTINGS_INFO_RECORDCONF_VALUS)){
                            feature.recordConf = true;
                        }

                        if (values.contains("no-media")){
                            feature.noMedia  = true;
                        }
                    }

                    TGLog.d("[DeviceFeature][Parse-RecordConf] final feature.recordConf = " + feature.recordConf);
                    TGLog.d("[DeviceFeature][Parse-RecordConf] final feature.supportTimeLapse = " + feature.supportTimeLapse);
                    break;
                case FEATURE_TYPE_AUTOTRACKING:
                    feature.autoTracking = isYes;
                    break;
                case FEATURE_TYPE_RESET:
                    feature.ptzReset = true;
                    feature.ptzReset2 = true;
                    break;
                case FEATURE_TYPE_SUPPORTPTZ:
                    if (isYes){
                        feature.supportPTZ = true;
                        feature.supportPTZ2 = true;
                    }else if (FEATURE_SUPPORT_NO.toLowerCase().equals(values)){
                        feature.supportPTZ = false;
                        feature.supportPTZ2 = false;
                    } else{
                        if (!values.startsWith("[")){
                            feature.supportPTZ = parseSupportPTZ(values );
                            feature.supportPTZ2 = feature.supportPTZ;
                            feature.presetPos = parsePresetPos(values);
                            feature.cruise = parseCruise(values);
                            feature.presetPos2 = feature.presetPos;
                            feature.watchPos = parseWatchPos(values);
                            feature.orientation = parseOrientation(values);
                            feature.orientation2 = feature.orientation;
                            feature.ptzReset = parseReset(values);
                            feature.ptzReset2 = feature.ptzReset;
                        }else{
                            TGLog.d("values = " + values);
                            List<String> list = JSON.parseArray(values, String.class);
                            if (list != null && list.size() >= 2){
                                feature.supportPTZ = parseSupportPTZ(list.get(0));
                                feature.supportPTZ2 = parseSupportPTZ(list.get(1));
                                feature.presetPos = parsePresetPos(list.get(0));
                                feature.presetPos2 = parsePresetPos(list.get(1));
                                feature.cruise = parseCruise(list.get(0));
                                feature.orientation = parseOrientation(list.get(0), 0);
                                feature.orientation2 = parseOrientation(list.get(1), 1);
                                feature.supportLocInPic = parseLocInPic(list.get(0));
                                feature.supportLocInPic2 = parseLocInPic(list.get(1));
                                feature.ptzReset = parseReset(list.get(0));
                                feature.ptzReset2 = parseReset(list.get(1));
                            }else if (list != null && list.size() == 1){
                                feature.supportPTZ = parseSupportPTZ(list.get(0));
                                feature.supportPTZ2 = false;
                                feature.presetPos = parsePresetPos(list.get(0));
                                feature.presetPos2 = false;
                                feature.cruise = parseCruise(list.get(0));
                                feature.orientation = parseOrientation(list.get(0));
                                feature.orientation2 =  -1;
                                feature.supportLocInPic = parseLocInPic(list.get(0));
                                feature.ptzReset = parseReset(list.get(0));
                                feature.ptzReset2 = false;
                            } else{
                                feature.supportLocInPic = false;
                                feature.supportPTZ = false;
                                feature.supportPTZ2= false;
                                feature.presetPos = false;
                                feature.presetPos2 = false;
                                feature.cruise = false;
                                feature.ptzReset = false;
                                feature.ptzReset2 = false;
                            }
                        }
                        feature.watchPos = parseWatchPos(values);

                        if (feature.watchPos){
                            feature.presetPos = true;
                            feature.presetPos2 = true;
                        }

                        if (feature.cruise){
                            feature.presetPos = true;
                            feature.presetPos2 = true;
                        }
                    }

                    break;
                case FEATURE_TYPE_DAYNIGHT:
                    feature.dayNight = isYes;
                    if (!isYes&&FEATURE_SUPPORT_AUTO.toLowerCase().equals(values)){
                        feature.dayNightAuto = true;
                        feature.dayNight = false;
                    }
                    break;
                case FEATURE_TYPE_DOUBLELIGHT:
                    feature.doubleLight = !FEATURE_SUPPORT_NO.toLowerCase().equals(values);
                    if (!StringUtils.isEmpty(values)){
                        feature.doubleLightTimer = values.toLowerCase().contains(FEATURE_SUPPORT_TIMER);
                        feature.doubleLightDim = values.toLowerCase().contains(FEATURE_SUPPORT_DIM);
                    }

                    break;
                case FEATURE_TYPE_MICROPHONE:
                    TGLog.d("[DeviceFeature][Parse-Microphone] values = " + values);
                    feature.microphoneInfo = values;
                    feature.supportMicrophone = true;
                    if (StringUtils.equalsIgnoreCase(FEATURE_SUPPORT_NO, values)){
                        feature.supportMicrophone = false;
                    }else if (!StringUtils.isEmpty(values)){
                        String[] list = values.split(",");
                        if (list.length > 0){
                            for (String s : list){
                                if (StringUtils.equalsIgnoreCase(s, DeviceFeature.FEATURE_SUPPORT_NO)){
                                    feature.supportMicrophone = false;
                                    break;
                                }
                            }
                        }
                    }

                    if (FEATURE_SUPPORT_TUNEVOL.toLowerCase().equals(values)) {
                        feature.supportMicrophoneTune = true;
                    } else if (FEATURE_SUPPORT_MUTEABLE.toLowerCase().equals(values)) {
                        feature.supportMicrophoneMuteable = true;
                    }

                    if (!feature.supportMicrophoneTune){
                        if (values.toLowerCase().contains(FEATURE_SUPPORT_TUNEVOL.toLowerCase())){
                            feature.supportMicrophoneTune = true;
                        }
                    }

                    if (!feature.supportMicrophoneMuteable){
                        if (values.toLowerCase().contains(FEATURE_SUPPORT_MUTEABLE.toLowerCase())){
                            feature.supportMicrophoneMuteable = true;
                        }
                    }

                    if (values.contains("aec")){
                        feature.isAec = true;
                    }

                    break;
                case FEATURE_TYPE_ALERTSOUND:
                    feature.supportBuzzer = isYes;
                    break;
                case FEATURE_TYPE_MOTION_DETECTION:
                    if (values.contains("sensitivity")) {
                        feature.supportMotionDetect = true;
                    }
                    if (values.contains("zone") || values.contains("visiblezone")) {
                        feature.supportDetectZone = true;
                    }
                    if (values.contains("visiblezone")) {
                        feature.supportDetectZoneVisible = true;
                    }
                    break;
                case FEATURE_TYPE_CAP_DEFENCE:
                    feature.capDefence = values;
                    feature.supportCapDefence = !FEATURE_SUPPORT_NO.toLowerCase().equals(values);
                    break;
                case FEATURE_TYPE_CAP_ZOOM:
                    TGLog.d("valus ===== " + values);
                    if (!values.startsWith("[")){
                        feature.support2Lenses = values.contains("2lenses");
                        feature.noShortcut = values.contains("no-shortcut");
                        feature.mfocus = values.contains("mfocus");
                        if (values.contains("times:")) {
                            feature.supportZoom = true;
                            if (values.contains("2lenses;")) {
                                values = values.replace("2lenses;", "");
                                values = values.replace("no-shortcut;", "");
                                values = values.replace(";no-shortcut", "");
                                values = values.replace("mfocus;", "");
                                values = values.replace(";mfocus", "");
                            }else if (values.contains("mfocus;")){
                                values = values.replace("mfocus;", "");
                            }else if (values.contains(";mfocus")){
                                values = values.replace(";mfocus", "");
                            }

                            feature.zoomMode = values;
                            feature.zoomMode2 = "";
                            if (feature.is2LensesCapZoom()) {
                                feature.lensesCapZoomDirvider();
                            }
                        } else {
                            feature.supportZoom = false;
                        }
                        TGLog.d("feature.supportZoom ============ " + feature.supportZoom);
                    }else{
                        List<String> list = JSON.parseArray(values, String.class);
                        if (list != null && list.size() >= 2){
                            feature.zoomMode = list.get(0);
                            feature.zoomMode2 = list.get(1);

                        }else if (list != null && list.size() == 1){
                            feature.zoomMode = list.get(0);
                            feature.zoomMode2 = "";
                        }

                        feature.support2Lenses = feature.zoomMode.contains("2lenses");
                        feature.noShortcut = feature.zoomMode.contains("no-shortcut");
                        feature.mfocus = values.contains("mfocus");
                        if (feature.zoomMode.contains("times:")) {
                            feature.supportZoom = true;
                            if (feature.is2LensesCapZoom()) {
                                feature.lensesCapZoomDirvider();
                            }
                        } else {
                            feature.supportZoom = false;
                        }
                    }
                    break;
                case FEATURE_TYPE_CAP_INSTRUCTIONS:
                    feature.supportCloseDevice = values.contains("close-device");
                    feature.supportDeviceBroadcast = values.contains("voice-prompt");
                    if (values.contains("alarm-bell")) {
                        feature.supportAlarmBell = true;
                    }
                    if (values.contains("pbrate")) {
                        feature.supportPbrate = true;
                    }
                    if (values.contains("alarmtone")) {
                        feature.supportAlarmTone = true;
                    }
                    if (values.contains("status-led")) {
                        feature.supportStatusLed = true;
                    }
                    if (values.contains("ai-switch")) {
                        feature.supportAiSwitch = true;
                    }
                    if (values.contains("temper")) {
                        feature.supportTemper = true;
                    }
                    if (values.contains("humidity")) {
                        feature.supportHumidity = true;
                    }
                    if (values.contains("ipconfig")) {
                        feature.supportIpConfig = true;
                    }

                    if (values.contains("parking-mon")){
                        feature.isSupportSettingCarParkingMonitor = true;
                    }
                    break;
                case FEATURE_TYPE_MULTICHANNELS:
                    feature.supportMultiChannels = TextUtils.equals(values, "2-composed");
                    break;
                case FEATURE_TYPE_DEVICE_TYPE:
                    feature.isDriveRec = TextUtils.equals(values, "driverec");
                    break;
                case FEATURE_G_SENSOR:
                    feature.isSupportCarParkingMonitor = values.contains("scene");
                    break;
                case FEATURE_TYPE_SPEAKER:
                    isYes = !FEATURE_SUPPORT_NO.toLowerCase().equals(values);
                    feature.supportSpeaker = isYes;
                    feature.supportSpeakerTune = FEATURE_SUPPORT_TUNEVOL.toLowerCase().equals(values);
                    TGLog.d("relMicPhone","feature.supportSpeaker = " + feature.supportSpeaker);
                    break;
                case FEATURE_TYPE_PIR:
                    feature.supportPIR = isYes;
                    break;
                case FEATURE_TYPE_ALARM_LIGHT:
                    feature.supportAlarmLight = isYes;
                    break;
                case FEATURE_TYPE_ROTATEVIDEO:
                    feature.rotateVideo = values;
                    break;
                case FEATURE_TYPE_RESOLUTION:
                    if (!TextUtils.isEmpty(values)) {
                        values = values.replaceAll("\\d+", "");
                        String[] strs = values.split("\\+");
                        feature.supportResolutionsData = values;
                        feature.supportResolutions = Arrays.asList(strs);
//                        Collections.sort(feature.supportResolutions);
                    }
                    break;
                case FEATURE_TYPE_BATTERYCAM:
                    feature.supportDormant = TextUtils.equals(values, "dormant");
                    feature.isBatteryCam = !FEATURE_SUPPORT_NO.toLowerCase().equals(values);
                    TGLog.d("=== getDeviceStatusInfo  BatteryCam isBatteryCam = " + values);
                    break;
                case FEATURE_TYPE_CAP_AI:
                    feature.capAI = values;
                    break;
            }

        }
        return feature;

    }

    ////-1 没云台 0 水平 1 垂直 2 全方位
    private static int parseOrientation(String values){
        if (StringUtils.isEmpty(values)){
            return 2;
        }
        if (values.equalsIgnoreCase(FEATURE_SUPPORT_NO)){
            return -1;
        }

        if (values.equalsIgnoreCase(FEATURE_VERTONLY)){
            return 1;
        }

        if (values.equalsIgnoreCase(FEATURE_HORZONLY)){
            return 0;
        }

        String[] list = values.split(",");
        if (list.length > 0){
            for (String s : list) {
                if (FEATURE_SUPPORT_NO.toLowerCase().equalsIgnoreCase(s)) {
                    return -1;
                }

                if (FEATURE_VERTONLY.toLowerCase().equalsIgnoreCase(s)){
                    return 1;
                }

                if (FEATURE_HORZONLY.toLowerCase().equalsIgnoreCase(s)){
                    return 0;
                }
            }

        }

        return 2;


    }


    private static boolean parseSupportPTZ(String data){
        if (StringUtils.isEmpty(data)){
            return false;
        }
        if (FEATURE_SUPPORT_NO.toLowerCase().equals(data)){
            return false;
        }
        String[] list = data.split(",");
        if (list.length > 0){
            for (String s : list) {
                if (FEATURE_SUPPORT_NO.toLowerCase().equals(s)) {
                    return false;
                }

                if (FEATURE_SUPPORT_YES.equalsIgnoreCase(s) || "Absolute".equalsIgnoreCase(s) || "Relative".equalsIgnoreCase(s)|| "Hybrid".equalsIgnoreCase(s)){
                    return true;
                }
            }
        }

        return false;
    }

    private static boolean parseSupportPTZ(String data, int index){
        if (StringUtils.isEmpty(data)){
            return false;
        }
        if (FEATURE_SUPPORT_NO.toLowerCase().equals(data)){
            return false;
        }
        String[] list = data.split(",");
        if (list.length > 0 && index < list.length){
            if (FEATURE_SUPPORT_NO.toLowerCase().equals(list[index])) {
                return false;
            }

            if (FEATURE_SUPPORT_YES.equalsIgnoreCase(list[index]) || "Absolute".equalsIgnoreCase(list[index]) || "Relative".equalsIgnoreCase(list[index]) || "Hybrid".equalsIgnoreCase(list[index])){
                return true;
            }
        }

        return false;
    }


    private static boolean parseLocInPic(String data){
        return !TextUtils.isEmpty(data) && data.contains(FEATURE_LOCINPIC.toLowerCase());
    }
    private static boolean parsePresetPos(String data){
        return !TextUtils.isEmpty(data) && data.contains(FEATURE_SUPPORT_PRESETPOS.toLowerCase());
    }

    private static boolean parseCruise(String data){
        return !TextUtils.isEmpty(data) && data.contains(FEATURE_SUPPORT_CRUISE.toLowerCase());
    }

    private static boolean parseWatchPos(String values){
        return !StringUtils.isEmpty(values) && values.toLowerCase().contains(FEATURE_WATCH_POS.toLowerCase());
    }

    private static boolean parseHorZOnly(String values){
        return !StringUtils.isEmpty(values) && values.toLowerCase().contains(FEATURE_HORZONLY.toLowerCase());
    }

    private static boolean parseVertOnly(String values){
        return !StringUtils.isEmpty(values) && values.toLowerCase().contains(FEATURE_VERTONLY.toLowerCase());
    }

    ////-1 没云台 0 水平 1 垂直 2 全方位
    private static int parseOrientation(String values, int index){
        if (StringUtils.isEmpty(values)){
            return 2;
        }
        if (values.equalsIgnoreCase(FEATURE_SUPPORT_NO)){
            return -1;
        }

        if (values.equalsIgnoreCase(FEATURE_VERTONLY)){
            return 1;
        }

        if (values.equalsIgnoreCase(FEATURE_HORZONLY)){
            return 0;
        }

        String[] list = values.split(",");
        if (list.length > 0 && index < list.length){
            String str = list[index];

            if (FEATURE_SUPPORT_NO.toLowerCase().equalsIgnoreCase(str)) {
                return -1;
            }

            if (FEATURE_VERTONLY.toLowerCase().equalsIgnoreCase(str)){
                return 1;
            }

            if (FEATURE_HORZONLY.toLowerCase().equalsIgnoreCase(str)){
                return 0;
            }
        }

        return 2;


    }


    private static boolean parseReset(String values){
        if (StringUtils.isEmpty(values)){
            return false;
        }
        if (values.toLowerCase().contains(FEATURE_SUPPORT_PRESETPOS.toLowerCase())){
            int index = values.toLowerCase().indexOf(FEATURE_TYPE_RESET.toLowerCase());
            index = values.toLowerCase().indexOf(FEATURE_TYPE_RESET.toLowerCase(), index + FEATURE_TYPE_RESET.length());
            return index != -1;
        }
        return values.toLowerCase().contains(FEATURE_TYPE_RESET.toLowerCase());
    }

    @NonNull
    public List<String> getSupportResolutions() {
        if (!TextUtils.isEmpty(supportResolutionsData) && supportResolutions == null) {
            supportResolutionsData = supportResolutionsData.replaceAll("\\d+", "");
            String[] strs = supportResolutionsData.split("\\+");
            supportResolutions = Arrays.asList(strs);
//            Collections.sort(supportResolutions);
        }
        return supportResolutions;
    }

    public int getQualityTypeIndex(int index) {
        if (supportResolutions != null) {
            int size = supportResolutions.size();
            if (index == 0) {
                return AVIOCTRLDEFs.AVIOCTRL_QUALITY_MAX;
            } else if (index == size - 1) {
                return AVIOCTRLDEFs.AVIOCTRL_QUALITY_MIN;
            } else {
                if (index == size / 2) {
                    return AVIOCTRLDEFs.AVIOCTRL_QUALITY_MIDDLE;
                } else if (index == 1) {
                    return AVIOCTRLDEFs.AVIOCTRL_QUALITY_HIGH;
                } else if (index == 3) {
                    return AVIOCTRLDEFs.AVIOCTRL_QUALITY_LOW;
                }
            }
        } else {
            if (index == 0) {
                return AVIOCTRLDEFs.AVIOCTRL_QUALITY_MAX;
            } else {
                return AVIOCTRLDEFs.AVIOCTRL_QUALITY_MIN;
            }
        }
        return AVIOCTRLDEFs.AVIOCTRL_QUALITY_MIN;
    }

    public boolean is2LensesCapZoom() {
        return supportZoom && support2Lenses;
    }

    public void lensesCapZoomDirvider() {
        lensesCapZoomDirviderImpl(zoomMode, 0);
        lensesCapZoomDirviderImpl(zoomMode2, 1);
    }

    public void lensesCapZoomDirviderImpl(String zoomModeString, int index) {
        TGLog.d("zoomMode = " + zoomModeString);
        if (TextUtils.isEmpty(zoomModeString) || zoomModeString.contains("unknown")||!zoomModeString.contains("times:") || zoomModeString.startsWith("[")) return;

        String text = zoomModeString.replace("times:", "").toUpperCase();
        String[] src = text.split(",");
        if (src.length>0){
            ZoomFeature zoomFeatureTmp = new ZoomFeature();
            zoomFeatureTmp.startAnalogZoom = src[0].startsWith("S");
            float startFactor = Float.parseFloat(StringUtils.replaceZoom(src[0]));
            if (src[0].contains("F")){
                String [] mulits = src[0].split("F");
                if (mulits.length==2){
                    String mult =  mulits[1];
                    zoomFeatureTmp.startFactor  = Float.parseFloat(mult);
                    zoomFeatureTmp.analogMultiple = (zoomFeatureTmp.startFactor-1)/(startFactor-1);
                    startFactor = zoomFeatureTmp.startFactor;
                }
            }
            if (src.length==3){
                zoomFeatureTmp.endAnalogZoom = src[1].startsWith("S");
                float maxFactor = startFactor * Float.parseFloat(StringUtils.replaceZoom(src[1]));
                zoomFeatureTmp.startFactor = startFactor;
                zoomFeatureTmp.maxFactor = maxFactor;
                BigDecimal b = BigDecimal.valueOf((startFactor - 1.0f) / (maxFactor - 1.0f));
                float tmp = b.setScale(6,  BigDecimal.ROUND_HALF_DOWN).floatValue();
                zoomFeatureTmp.zoomDivider = tmp;
                zoomFeatureTmp.steps = Integer.parseInt(src[2]);
            }else {
                zoomFeatureTmp.maxFactor = zoomFeatureTmp.startFactor;
                zoomFeatureTmp.steps = (int) zoomFeatureTmp.maxFactor;
                zoomFeatureTmp.endAnalogZoom = zoomFeatureTmp.startAnalogZoom;
            }

            zoomFeatureTmp.index = index;
            if (index == 0){
                zoomFeature = zoomFeatureTmp;
            }else{
                zoomFeature2 = zoomFeatureTmp;
            }
        }
    }

    public static int getRotation(String abilities){
        if (TextUtils.isEmpty(abilities)){
            return 0;
        }

        JSONObject jsonObject = JSONObject.parseObject(abilities.toLowerCase());
        if (jsonObject != null){
            String rotateVideo = jsonObject.getString(FEATURE_TYPE_ROTATEVIDEO.toLowerCase());
            if (StringUtils.equalsIgnoreCase(rotateVideo, FEATURE_TYPE_ROTATEVIDEO_CW90)){
                return 90;
            }
            if (StringUtils.equalsIgnoreCase(rotateVideo, FEATURE_TYPE_ROTATEVIDEO_CW180)){
                return 180;
            }
            if (StringUtils.equalsIgnoreCase(rotateVideo, FEATURE_TYPE_ROTATEVIDEO_CW270)){
                return 270;
            }
        }
        return 0;
    }


    @NonNull
    public ZoomFeature getZoomFeature(int index) {
        lensesCapZoomDirvider();
        return index == 0 ? zoomFeature : zoomFeature2;
    }


    public boolean isDayNightOnly() {
        return dayNight&&!doubleLight;
    }

    public boolean isDoubleLightOnly() {
        return doubleLight&&!dayNight;
    }

    public static class ZoomFeature {
        private static final String TAG = "ZoomFeature";
        public float maxFactor;
        public float startFactor;
        public int steps;
        public boolean startAnalogZoom;
        public boolean endAnalogZoom;
        public float zoomDivider;
        public float analogMultiple = 1;
        public int index = 0;
        public boolean isAnalogZoom(float scale) {
            TGLog.d(TAG, "scale "+scale+" zoomDivider: "+zoomDivider);
            if (isInEndArea(scale)){
                return  endAnalogZoom;
            }
            if (scale<zoomDivider){
                return  startAnalogZoom;
            }
            return false;
        }
        public boolean isInEndArea(float scale){
            if (scale>zoomDivider||maxFactor==startFactor)return true;
            float diff = (zoomDivider - scale)*1000000;
           return  Math.abs(diff)<1;
        }
        public boolean isSingleZoom(){
            return  maxFactor==startFactor&&startAnalogZoom;
        }
    }
}
